using System;
using System.IO;
using UnityEngine;

[Serializable]
public class buffer_class
{
	public FileStream file;

	public Vector2 resolution;

	public byte[] bytes;

	public ulong length;

	public Vector2 size;

	public tile_class tiles;

	public ulong pos;

	public ulong row;

	public Rect innerRect;

	public Rect outerRect;

	public Vector2 offset;

	public int radius;

	public buffer_class()
	{
		tiles = new tile_class();
	}

	public void init()
	{
		tiles.x = (int)Mathf.Ceil(resolution.x / size.x);
		tiles.y = (int)Mathf.Ceil(resolution.x / size.y);
		row = (ulong)(resolution.x * 3f);
	}

	public void getRects(tile_class tile)
	{
		int num = radius + 20;
		innerRect.x = (float)tile.x * size.x - 5f;
		innerRect.y = (float)tile.y * size.y - 5f;
		innerRect.width = size.x + 10f;
		innerRect.height = size.y + 10f;
		if (!(innerRect.xMin >= 0f))
		{
			innerRect.xMin = 0f;
		}
		if (!(innerRect.yMin >= 0f))
		{
			innerRect.yMin = 0f;
		}
		if (!(innerRect.xMax <= resolution.x))
		{
			innerRect.xMax = resolution.x;
		}
		if (!(innerRect.yMax <= resolution.y))
		{
			innerRect.yMax = resolution.y;
		}
		outerRect.xMin = innerRect.xMin - (float)num;
		outerRect.yMin = innerRect.yMin - (float)num;
		outerRect.xMax = innerRect.xMax + (float)num;
		outerRect.yMax = innerRect.yMax + (float)num;
		if (!(outerRect.xMin >= 0f))
		{
			outerRect.xMin = 0f;
		}
		else if (!(outerRect.xMax <= resolution.x))
		{
			outerRect.xMax = resolution.x;
		}
		if (!(outerRect.yMin >= 0f))
		{
			outerRect.yMin = 0f;
		}
		else if (!(outerRect.yMax <= resolution.y))
		{
			outerRect.yMax = resolution.y;
		}
		length = (ulong)(outerRect.width * outerRect.height * 3f);
		offset.x = innerRect.x - outerRect.x;
		offset.y = innerRect.y - outerRect.y;
		if (bytes == null)
		{
			bytes = new byte[(uint)length];
		}
		if ((ulong)bytes.Length != length)
		{
			bytes = new byte[(uint)length];
		}
	}

	public void read()
	{
		for (int i = 0; (float)i < outerRect.height; i++)
		{
			pos = (ulong)((float)(long)row * outerRect.y + (float)((long)row * (long)i) + outerRect.x * 3f);
			file.Seek((long)pos, SeekOrigin.Begin);
			file.Read(bytes, (int)(outerRect.width * (float)i * 3f), (int)(outerRect.width * 3f));
		}
	}

	public void write()
	{
		for (int i = 0; (float)i < innerRect.height; i++)
		{
			pos = (ulong)((float)(long)row * innerRect.y + (float)((long)row * (long)i) + innerRect.x * 3f);
			file.Seek((long)pos, SeekOrigin.Begin);
			file.Write(bytes, (int)(outerRect.width * (float)i * 3f + outerRect.width * 3f * offset.y + offset.x * 3f), (int)(innerRect.width * 3f));
		}
	}

	public void copy_bytes(byte[] bytes1, byte[] bytes2)
	{
		ulong num = 0uL;
		while ((long)num < (long)bytes1.Length)
		{
			bytes2[(uint)num] = bytes1[(uint)num];
			num++;
		}
	}

	public void clear_bytes()
	{
		ulong num = 0uL;
		while ((long)num < (long)bytes.Length)
		{
			bytes[(uint)num] = 0;
			num++;
		}
	}
}
